package com.stx.friends.dao.impl;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import com.stx.friends.dao.BloggerDao;
import com.stx.friends.entity.BloggerEntity;
import com.stx.friends.vo.BloggerVo;

public class BloggerDaoImpl extends JdbcDaoSupport implements BloggerDao {
	public List<BloggerEntity> list(BloggerVo vo) {

		String sql = "select * from wrx_blogger";

		JdbcTemplate jt = getJdbcTemplate();

		List<BloggerEntity> bloggerList = jt.query(sql, new BloggerRowMapper());

		return bloggerList;
	}

	class BloggerRowMapper implements RowMapper {
		@Override
		public Object mapRow(ResultSet rs, int rowNum) throws SQLException {
			
			BloggerEntity entity = new BloggerEntity();

			entity.setBid(rs.getString("bid"));
			entity.setCid(rs.getString("cid"));
			entity.setCreateTime(rs.getString("createTime"));
			entity.setDelFlag(rs.getString("delFlag"));
			entity.setReadable(rs.getString("readable"));
			entity.setTitle(rs.getString("title"));
			entity.setWrxContent(rs.getString("wrxContent"));
			entity.setWrxLevel(rs.getString("wrxLevel"));
			entity.setWrxUid(rs.getString("wrxUid"));

			return entity;
		}
	}
	public BloggerEntity getBloggerById(String bid){
		String sql = "select * from wrx_blogger where bid="+bid;

		JdbcTemplate jt = getJdbcTemplate();
		
		return (BloggerEntity) jt.query(sql, new BloggerRowMapper()).get(0);
		
	}
	@Override
	public void delBlogger(String bid) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "delete from wrx_blogger where bid = ?";

		// 构建参数数组
		Object[] args = { bid };

		jt.update(sql, args);

	}

	@Override
	public void addBlogger(BloggerVo blogger) {

		JdbcTemplate jt = getJdbcTemplate();

		String sql = "insert into wrx_blogger (bid,wrxUid,title,wrxContent,cid) values(seq_wrx_blogger.nextval,?,?,?,?)";

		// 构建参数数组
		Object[] args = {blogger.getWrxUid(),blogger.getTitle(),
				blogger.getWrxContent(), blogger.getCid(),
						
				 };

		jt.update(sql, args);

	}
 
	@Override
	public void updateBlogger(BloggerVo blogger) {
		JdbcTemplate jt = getJdbcTemplate();
		
		String sql = "update  wrx_blogger set title=?,wrxContent=? where bid=?" ;

		// 构建参数数组
		Object[] args = {blogger.getTitle(),
				blogger.getWrxContent(),blogger.getBid() };

		jt.update(sql, args);
		
	}
	@Override
	public List<BloggerEntity> listLike(BloggerVo vo) {

		String sql = "select * from wrx_blogger where wrxlevel=1";

		JdbcTemplate jt = getJdbcTemplate();

		List<BloggerEntity> bloggerList = jt.query(sql, new BloggerRowMapper());

		return bloggerList;
	}
	@Override
	public void likeBlogger(String bid) {
		String sql="update WRX_BLOGGER set wrxlevel=1 where bid=?";
		JdbcTemplate jt = getJdbcTemplate();
		Object[] args={bid};
		jt.update(sql, args);
		
	}
	@Override
	public void unLikeBlogger(String bid) {
		String sql="update WRX_BLOGGER set wrxlevel=0 where bid=?";
		JdbcTemplate jt = getJdbcTemplate();
		Object[] args={bid};
		jt.update(sql, args);
		
	}
	@Override
	public List<BloggerEntity> listByUid(String wrxuid) {
		List<BloggerEntity> bloggerList=null;
		if(wrxuid.equals("")||wrxuid==null){
			

			return bloggerList;
		}
		else{
String sql = "select * from wrx_blogger where wrxuid="+wrxuid;
			
			JdbcTemplate jt = getJdbcTemplate();
			bloggerList = jt.query(sql, new BloggerRowMapper());
			return bloggerList;
		}
		
		
	}

	
}
